package com.myk.game.gobangchess.rooms;

import com.myk.game.gobangchess.constants.BoardStyleData;
import com.myk.game.gobangchess.constants.WinMode;
import com.myk.game.gobangchess.utils.GridsUtils;
import com.myk.game.gobangchess.Room;
import com.myk.game.gobangchess.signs.AroundSign;

import java.util.ArrayList;
import java.util.List;

/**
 * 传统五子棋房间
 */
public class GoBangRoom extends Room {

    public GoBangRoom(String roomName) {
        super(roomName, "\t\t传统的五子棋，轮流落子，连五获胜，没有禁手和交换规则。","五子棋", BoardStyleData.getGoBangStyle(15,15), true, true);
    }

    @Override
    public String getRuleText() {
        return "黑白双方轮流落子。\n" +
                "获胜条件：形成五子一线\n" +
                "和棋条件：无棋可走";
    }

    @Override
    public void doRule(int row, int col, int value) {
        if (curGrids[row][col] == 0) { //只能落在空位置
            signList.clear();
            List<Integer> ids = new ArrayList<>();
            ids.add(getIndex(row,col));
            signList.add(new AroundSign(ids,"#00000000","#6600FF00",1.2f));
            curGrids[row][col] = value; //实际落子
            stepAdd();; //步数统计加增加

            //黑方落子时
            if (value == 1) {
                if (checkFormation(1) > 0) {//黑方连5成功
                    doOver(WinMode.BLACK_WIN,player_black.getNickNameOrTemp() + "执黑棋战胜" + player_white.getNickNameOrTemp());
                }
                else {
                    int blankNum = GridsUtils.getDataCount(curGrids, 0);
                    if (blankNum == 0) {//没有胜负也没有空子了算战平
                        doOver(WinMode.BOTH_DRAW,player_black.getNickNameOrTemp() + "-黑棋战平" + player_white.getNickNameOrTemp() + "-白棋");
                    }
                    else {
                        noticeWhiteMove();
                    }
                }
            }

            //白方落子时
            else if (value == 2) {
                if (checkFormation(2) > 0) {
                    doOver(WinMode.WHITE_WIN,player_white.getNickNameOrTemp() + "执白棋战胜" + player_black.getNickNameOrTemp());
                }
                else {
                    int blankNum = GridsUtils.getDataCount(curGrids, 0);  //没有胜负也没有空子了算战平
                    if (blankNum == 0) {
                        doOver(WinMode.BOTH_DRAW,player_black.getNickNameOrTemp() + "-黑棋战平" + player_white.getNickNameOrTemp() + "-白棋");
                    }
                    else {
                        noticeBlackMove();
                    }
                }
            }

        }
    }

    //获取成5的次数
    private int checkFormation(int value) {
        int rows = curGrids.length;
        int cols = curGrids[0].length;
        int formationCount = 0;

        // 水平方向
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols - 4; j++) {
                int length = 0;
                for (int k = j; k < j + 5; k++) {
                    if (curGrids[i][k] == value) {
                        length++;
                    }
                    else {
                        break;
                    }
                }
                if (length >= 5) {
                    formationCount += (length - 4);
                }
            }
        }

        // 垂直方向
        for (int i = 0; i < rows - 4; i++) {
            for (int j = 0; j < cols; j++) {
                int length = 0;
                for (int k = i; k < i + 5; k++) {
                    if (curGrids[k][j] == value) {
                        length++;
                    }
                    else {
                        break;
                    }
                }
                if (length >= 5) {
                    formationCount += (length - 4);
                }
            }
        }

        // 左上到右下方向
        for (int i = 0; i < rows - 4; i++) {
            for (int j = 0; j < cols - 4; j++) {
                int length = 0;
                for (int k = 0; k < 5; k++) {
                    if (curGrids[i + k][j + k] == value) {
                        length++;
                    }
                    else {
                        break;
                    }
                }
                if (length >= 5) {
                    formationCount += (length - 4);
                }
            }
        }

        // 右上到左下方向
        for (int i = 0; i < rows - 4; i++) {
            for (int j = 4; j < cols; j++) {
                int length = 0;
                for (int k = 0; k < 5; k++) {
                    if (curGrids[i + k][j - k] == value) {
                        length++;
                    }
                    else {
                        break;
                    }
                }
                if (length >= 5) {
                    formationCount += (length - 4);
                }
            }
        }

        return formationCount;
    }
}
